package Backend;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import GUI.Cell;

public class ImageOutPut {
	
	public static void generatePicture(File path, Cell[][] cells, int cellwidth){
		
		BufferedImage bufimg = new BufferedImage(cellwidth * cells[0].length, cellwidth * cells.length, BufferedImage.TYPE_INT_RGB);
		Graphics2D createGraphics = bufimg.createGraphics();
		

		
		createGraphics.setColor(Color.WHITE);
		createGraphics.fillRect(0, 0, (cellwidth * cells[0].length), (cellwidth * cells.length));
		
		for(int i=0; i<cells.length; i++){
			for(int j=0; j<cells[i].length; j++){
				paintCell(createGraphics, cells[i][j], cellwidth);
			}
		}
		
		createGraphics.dispose();

		
		try {
			ImageIO.write(bufimg, "png" , path);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void paintCell(Graphics2D graphic, Cell c, int width){
		float f = (float) 2;
		BasicStroke bs = new BasicStroke(f);
		graphic.setStroke(bs);
		graphic.setColor(Color.black);
		int xoffset = c.getPosx() * width;
		int yoffset = c.getPosy() * width;
		
		if(c.isTop()){
			graphic.drawLine(xoffset, yoffset, xoffset+width, yoffset);
		}
		if(c.isLeft()){
			graphic.drawLine(xoffset, yoffset, xoffset, yoffset+width);
		}
		if(c.isRight()){
			graphic.drawLine(width+xoffset, yoffset, width+xoffset, width+yoffset);
		}
		if(c.isBottom()){
			graphic.drawLine(xoffset, width+yoffset, width+xoffset, width+yoffset);
		}
		
		
	}
	
}
